<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined('_JEXEC') or die('Restricted access');

class Images{
	function ReadImages( $imagePath, $folderPath, &$folders, &$images ){
		$imgFiles = $this->JReadDirectory( $imagePath );
		foreach ($imgFiles as $file) {
			$ff_ 	= $folderPath . $file .'/';
			$ff 	= $folderPath . $file;
			$i_f 	= $imagePath .'/'. $file;

			if ( is_dir( $i_f ) && $file != 'CVS' && $file != '.svn') {
				$folders[] = JHTML::_('select.option', $ff_ );
				$this->ReadImages( $i_f, $ff_, $folders, $images );
			} 
			else if ( preg_match("#gif|jpg|png#i", $file ) && is_file( $i_f ) ) {			
				$imageFile = substr( $ff, 1 );
				$images[$folderPath][] = JHTML::_('select.option', $imageFile, $file );
			}
		}
	}
	
	function getFolders(&$folders, $path, $images){
		$javascript = "onchange=\"changeDynaList( 'imagefiles', folderimages, document.adminForm.folders.options[document.adminForm.folders.selectedIndex].value, 0, 0);  previewImage( 'imagefiles', 'view_imagefiles', '$path/' );\"";
		$getfolders = JHTML::_('select.genericlist',   $folders, 'folders', ' size="1" '. $javascript, 'value', 'text', '/' );
		
		return $getfolders;
	}
		
	function JReadDirectory($path, $filter='.', $recurse=false, $fullpath=false){
		$arr = array();
		if(!@is_dir($path)){
			return $arr;
		}
		$handle = opendir( $path );
	
		while($file = readdir($handle)) {
			$dir = JPath::clean( $path.'/'.$file, false );
			$isDir = is_dir( $dir );
			if (($file != ".") && ($file != "..")) {
				if (preg_match( "/$filter/", $file )) {
					if ($fullpath) {
						$arr[] = trim( mosPathName( $path.'/'.$file, false ) );
					} else {
						$arr[] = trim( $file );
					}
				}
				if ($recurse && $isDir) {
					$arr2 = JReadDirectory( $dir, $filter, $recurse, $fullpath );
					$arr = array_merge( $arr, $arr2 );
				}
			}
		}
		closedir($handle);
		asort($arr);
		return $arr;
	}
	
	function getImageFiles(&$images, $path){
		if(!isset($images['/'])){
			$images['/'][] = JHTML::_('select.option', '');
		}	
		$javascript	= "onchange=\"previewImage( 'imagefiles', 'view_imagefiles', '$path/')\"";
		$getimages	= JHTML::_('select.genericlist',   $images['/'], 'imagefiles', ' size="10" multiple="multiple" '. $javascript , 'value', 'text', null);
		return $getimages;
	}
	
	function GetSavedImages($survey_id, $path, $type=""){
		$images2 = array();
		$result = $this->getSurveyImages($type);
		
		foreach($result as $key=>$file){	
			$temp = explode('|', $file["imagelist"]);
			if(strrchr($temp[0], '/')){
				$filename = substr(strrchr($temp[0], '/'), 1);
			} 
			else{
				$filename = $temp[0];
			}
			$images2[] = JHTML::_('select.option',  $file["imagelist"], $filename);
		}
		
		$javascript	= "onchange=\"previewImage( 'imagelist', 'view_imagelist', '".$path."/' ); \"";
		$imagelist 	= JHTML::_('select.genericlist',   $images2, 'imagelist', ' size="10" '. $javascript, 'value', 'text' );

		return $imagelist;
	}
	
	function getSurveyImages($type=""){
		if($type == "surveys"){		
			$db =& JFactory::getDBO();
			$id = JRequest::getVar("id");
			$query = $db->getQuery(true);
			$query->clear();
			$query->select('imagelist');
			$query->from('#__ijoomla_surveys_surveys');
			$query->where("id=".intval($id));					
			$db->setQuery($query);
			$db->query();
			$result = $db->loadAssocList();			
			return $result;	
		}
		elseif($type == "pages"){
			$db =& JFactory::getDBO();
			$id = JRequest::getVar("id");
			$query = $db->getQuery(true);
			$query->clear();
			$query->select('imagelist');
			$query->from('#__ijoomla_surveys_pages');
			$query->where("id=".intval($id));					
			$db->setQuery($query);
			$db->query();
			$result = $db->loadAssocList();
			return $result;	
		}		
	}
};

?>